package com.xczl.demo.test;

import org.apache.commons.lang3.RandomUtils;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.transaction.annotation.Transactional;

import com.xczl.demo.domain.User;
import com.xczl.demo.service.UserService;


@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration({ "classpath:root-context.xml" })
public class UserServiceTestCase {

	@Autowired
	private UserService userService;

	@Test
	@Transactional
	public void add() {
		//插入
		User user = createUser();
		userService.addUser(user);
		User user2 = userService.findById(user.getUserId());
		Assert.assertNotNull(user2);
	}

	private User createUser() {
		User user = new User();
		user.setUserName("user" + RandomUtils.nextInt(1000, 1500));
		user.setPassword("123456");
		user.setSex(user.SEX_MALE);
		user.setName("小明");
		return user;
	}

	@Test
	@Transactional
	public void login() {
		//注册
		User user = createUser();
		userService.addUser(user);
		//登录
		User user2 = userService.login(user.getUserName(), user.getPassword());
		Assert.assertNotNull(user2);
	}

}
